package app.project.directionlensproject.map;

import java.util.Locale;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import app.project.directionlensproject.R;
import app.project.directionlensproject.model.Place;
import app.project.directionlensproject.util.BundleKeys;
import app.project.directionlensproject.util.CommonUtils;
import app.project.directionlensproject.util.Constants;
import app.project.directionlensproject.util.Utilities;

public class PlaceDetailActivity extends Activity implements OnClickListener {

	private GoogleMap map;
	public static final LatLng HANOI = new LatLng(20.993776, 105.811417);
	private TextView mTextViewPlaceName;
	private TextView mTextViewInfor,mTextViewAddress,mTextViewMobile,mTextViewMobilePhone,mTextViewFax,mTextViewVideo,mTextViewWebsite,mTextViewEmail;
	private ImageView mImageViewPlace,mImageViewMobile,mImageViewMobilePhone;
	private Button mButtonDirection;
	private Place myPlace;
	private int categoryID;
	private String description;
	@Override
	public void onCreate(Bundle circle) {
		super.onCreate(circle);
		
		boolean isEn = CommonUtils.getBooleanSetting(this, Constants.IS_EN, false);
		if (!isEn) {
			Configuration c = new Configuration(getResources().getConfiguration());
			c.locale = new Locale("vi");
			getResources().updateConfiguration(c, getResources().getDisplayMetrics());
		} else {
			Configuration c = new Configuration(getResources().getConfiguration());
			c.locale = Locale.ENGLISH;
			getResources().updateConfiguration(c, getResources().getDisplayMetrics());
		}
		
		setContentView(R.layout.activity_place_detail);
		myPlace = (Place) getIntent().getSerializableExtra("place");
		categoryID=getIntent().getIntExtra(BundleKeys.CATE_ID,0);
		createViewInit(myPlace);
		createMapView1(myPlace);
	}
	
	
	/** create view init image place,description,title of place
	 * @param place
	 */
	private void createViewInit(final Place place){
		mTextViewInfor=(TextView)findViewById(R.id.textViewPlaceInfor);
		mTextViewPlaceName = (TextView) findViewById(R.id.textViewNamePlace);
		mTextViewAddress=(TextView)findViewById(R.id.textViewPlaceAddress);		
		mTextViewMobile=(TextView)findViewById(R.id.textViewPlaceMobile);
		mTextViewMobilePhone=(TextView)findViewById(R.id.textViewPlaceMobilePhone);
		mTextViewFax=(TextView)findViewById(R.id.textViewPlaceFax);
		mTextViewVideo=(TextView)findViewById(R.id.textViewPlaceVideo);
		mTextViewWebsite=(TextView)findViewById(R.id.textViewPlaceWebsite);
		mTextViewEmail=(TextView)findViewById(R.id.textViewPlaceEmail);
		mImageViewPlace=(ImageView)findViewById(R.id.imageViewPlace);
		mImageViewMobile=(ImageView)findViewById(R.id.imageViewMobile);
		mImageViewMobilePhone=(ImageView)findViewById(R.id.imageViewMobilePhone);
		mButtonDirection=(Button)findViewById(R.id.buttonPlaceDirection);	
	
		//set value for all textview
		mTextViewPlaceName.setText(place.placename);
		description=place.description;
		Log.e("Description:",description);
		
		mTextViewInfor.setText(Utilities.getInstance().getValueByType(Constants.TYPE_INFO,description));
		mTextViewAddress.setText(Utilities.getInstance().getValueByType(Constants.TYPE_ADDRESS,description));
		mTextViewMobile.setText(Utilities.getInstance().getValueByType(Constants.TYPE_MOBILE,description).replace("/", "\n"));
		mTextViewMobilePhone.setText(Utilities.getInstance().getValueByType(Constants.TYPE_MOBILE_PHONE,description));
		mTextViewFax.setText(Utilities.getInstance().getValueByType(Constants.TYPE_FAX,description));
		mTextViewVideo.setText(Utilities.getInstance().getValueByType(Constants.TYPE_VIDEO,description));
		mTextViewWebsite.setText(Utilities.getInstance().getValueByType(Constants.TYPE_WEBSITE,description));
		//mTextViewWebsite.setMovementMethod(LinkMovementMethod.getInstance());
		mTextViewEmail.setText(Utilities.getInstance().getValueByType(Constants.TYPE_EMAIL,description));
		
		if(place.getBitmap()==null){
			mImageViewPlace.setBackgroundResource(R.drawable.no_image);
		}else{
			mImageViewPlace.setImageBitmap(place.getBitmap());
		}
		
		if(Utilities.getInstance().getValueByType(Constants.TYPE_MOBILE,description).trim().equals("")){
			mImageViewMobile.setVisibility(View.GONE);
		}else{
			mImageViewMobile.setVisibility(View.VISIBLE);
		}
		if(Utilities.getInstance().getValueByType(Constants.TYPE_MOBILE_PHONE,description).trim().equals("")){
			mImageViewMobilePhone.setVisibility(View.GONE);
		}else{
			mImageViewMobilePhone.setVisibility(View.VISIBLE);
		}
		
		
		mButtonDirection.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent it=new Intent(PlaceDetailActivity.this,PlaceDirectionActivity.class);
				it.putExtra("place", place);
				it.putExtra(BundleKeys.CATE_ID, categoryID);
				startActivity(it);
			}
		});
		
		//set event click textview
		mTextViewMobile.setOnClickListener(this);	
		mTextViewMobilePhone.setOnClickListener(this);
		mTextViewVideo.setOnClickListener(this);
		mTextViewWebsite.setOnClickListener(this);
		mTextViewEmail.setOnClickListener(this);
		mImageViewMobile.setOnClickListener(this);
		mImageViewMobilePhone.setOnClickListener(this);
	}
	
	/**
	 * create mapview when device parallel of land
	 */
	private void createMapView1(final Place place) {
		
		LatLng myLatLng=new LatLng(place.locationx, place.locationy);
		MarkerOptions mkOption = new MarkerOptions()
				.position(myLatLng)
				.title(place.placename)
				.snippet(place.description)
				.icon(Utilities.getInstance().getBitmapDescriptionFromType(place.categoryid));
		if(place.categoryid==0){
			mkOption.icon(Utilities.getInstance().getBitmapDescriptionFromType(categoryID));
		}

		map = ((MapFragment) getFragmentManager().findFragmentById(
				R.id.mapPlaceDetail)).getMap();

		// add maker to map
		map.addMarker(mkOption);

		// Move the camera instantly to hamburg with a zoom of 15.
		map.moveCamera(CameraUpdateFactory.newLatLngZoom(myLatLng, 20));

		// Zoom in, animating the camera.
		map.animateCamera(CameraUpdateFactory.zoomTo(11), 2000, null);
		
		map.setOnMapClickListener(new OnMapClickListener() {

			public void onMapClick(LatLng point) {				
				// TODO Auto-generated method stub
				Log.e("MapClick","true");
				Intent it=new Intent(PlaceDetailActivity.this,MapLargeActivity.class);
				it.putExtra("place", place);
				startActivity(it);				
			}
		});
	}


	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.textViewPlaceMobile:
			//Utilities.getInstance().callMobilePlace(PlaceDetailActivity.this, mTextViewMobile.getText().toString());			
			if(!Utilities.getInstance().getValueByType(Constants.TYPE_MOBILE,description).toString().trim().equals("")){
					Utilities.getInstance().callMobilePlace(PlaceDetailActivity.this, Utilities.getInstance().getValueByType(Constants.TYPE_MOBILE,description));	
			}
			Log.e("MobilePhoneABC",Utilities.getInstance().getValueByType(Constants.TYPE_MOBILE,description));
			break;
		case R.id.textViewPlaceMobilePhone:
			if(!Utilities.getInstance().getValueByType(Constants.TYPE_MOBILE_PHONE,description).trim().equals(""))
			{
				Utilities.getInstance().callMobilePlace(PlaceDetailActivity.this, Utilities.getInstance().getValueByType(Constants.TYPE_MOBILE_PHONE,description));
			}			
			Log.e("MobilePhoneABC",Utilities.getInstance().getValueByType(Constants.TYPE_MOBILE,description));
			break;
		case R.id.imageViewMobile:
			if(!Utilities.getInstance().getValueByType(Constants.TYPE_MOBILE,description).trim().equals("")){
				Utilities.getInstance().callMobilePlace(PlaceDetailActivity.this, Utilities.getInstance().getValueByType(Constants.TYPE_MOBILE,description));
			}
			break;
		case R.id.imageViewMobilePhone:
			if(!Utilities.getInstance().getValueByType(Constants.TYPE_MOBILE_PHONE,description).trim().equals("")){
				Utilities.getInstance().callMobilePlace(PlaceDetailActivity.this, Utilities.getInstance().getValueByType(Constants.TYPE_MOBILE_PHONE,description));
			}
			break;
		case R.id.textViewPlaceVideo:
			if(!mTextViewVideo.getText().toString().trim().equals("")){
				Utilities.getInstance().openWeb(PlaceDetailActivity.this, mTextViewVideo.getText().toString());
			}			
			break;
		case R.id.textViewPlaceWebsite:
			if(!mTextViewWebsite.getText().toString().trim().equals("")){
				Utilities.getInstance().openWeb(PlaceDetailActivity.this, mTextViewWebsite.getText().toString());
			}
			
			break;
		case R.id.textViewPlaceEmail:
			if(!mTextViewEmail.getText().toString().trim().equals("")){
				Utilities.getInstance().sendMail(PlaceDetailActivity.this,mTextViewEmail.getText().toString());
			}
			
			break;
		default:
			break;
		}
	}
}
